package com.wjc.myktapplication.page

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.wjc.basemodule.base.BaseFragment
import com.wjc.basemodule.base.DataBindingConfig
import com.wjc.basemodule.utils.LogTools
import com.wjc.common.doSelected
import com.wjc.common.initFragment
import com.wjc.myktapplication.R
import com.wjc.myktapplication.databinding.FragmentMainHomeBinding
import com.wjc.myktapplication.page.me.MeFragment
import com.wjc.myktapplication.page.project.ProjectMainFragment
import com.wjc.myktapplication.page.search.SearchFragment
import com.wjc.myktapplication.page.wxaccount.AccountMainFragment
import com.wjc.myktapplication.page.wxaccount.WxArtFragment
import kotlinx.coroutines.flow.Flow

// Home 主页
class MainHomeFragment : BaseFragment<FragmentMainHomeBinding>() {
    private lateinit var onPageChangeCallback: ViewPager2.OnPageChangeCallback
    private val fragmentList = arrayListOf<Fragment>()
    lateinit var flow: Flow<Int>


    override val dataBindingConfig: DataBindingConfig
        get() = DataBindingConfig(R.layout.fragment_main_home)


    private val accountMainFragment by lazy { AccountMainFragment() }
    private val projectMainFragment by lazy { ProjectMainFragment() }
    private val searchFragment by lazy { SearchFragment() }
    private val meFragment by lazy { MeFragment() }


    init {
        fragmentList.apply {
            add(accountMainFragment)
            add(projectMainFragment)
            add(searchFragment)
            add(meFragment)
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }


    private fun initView() {
        //初始化viewpager
        binding.vpHome.adapter = object : FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {
            override fun createFragment(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getItemCount(): Int {
                return fragmentList.size
            }
        }

        onPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.btnNav.menu.getItem(position).isChecked = true
            }
        }

        binding.vpHome.registerOnPageChangeCallback(onPageChangeCallback)

//       binding.vpHome.isUserInputEnabled = false //禁止自己滑动

        //初始化底部导航栏
        binding.btnNav.run {
            setOnNavigationItemSelectedListener { item ->
                when (item.itemId) {
//                    R.id.menu_home -> {
//                        binding.vpHome.setCurrentItem(0, false)
//                    }
                    R.id.projectMainFragment -> binding.vpHome.setCurrentItem(1, false)
                    R.id.menu_square -> binding.vpHome.setCurrentItem(2, false)
                    R.id.menu_official_account -> binding.vpHome.setCurrentItem(0, false)
                    R.id.menu_mine -> binding.vpHome.setCurrentItem(3, false)
                }
                // 这里注意返回true,否则点击失效
                true
            }
        }
    }

    override fun onDestroyView() {
        binding.vpHome.unregisterOnPageChangeCallback(onPageChangeCallback)
        super.onDestroyView()
    }
}